using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using The_Last_Knights.Event;


namespace The_Last_Knights.GUI
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Element2D : VisibleGameEntity
    {        
        #region Attributes
        protected Vector2 _position;
        protected Vector2 _size;
        protected Texture2D _background;
        protected string _id;
        protected float _opacity;
        #endregion

        #region Properties
        public string Id
        {
            get { return _id; }            
        }

        public float Opacity
        {
            get { return _opacity; }
            set { _opacity = value; }
        }

        public Vector2 Position
        {
            get { return _position; }
        }

        public Vector2 Size
        {
            get { return _size; }
        }

        public Texture2D Background
        {
            get { return _background; }
            set { _background = value; }
        }
#endregion

        public Element2D(Game game, string ID, Vector2 pos, Vector2 size, Texture2D background)
            : base(game)
        {
            _id = ID;
            _position = pos;
            _size = size;
            _background = background;
            _opacity = 1.0f;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw it self
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (_background != null)
            {
                SpriteBatch sb = GetSpriteBatch();
                if (sb != null)
                {                    
                    sb.Begin();
                    sb.Draw(_background, new Rectangle((int)_position.X, (int)_position.Y, (int)_size.X, (int)_size.Y), new Color(255, 255, 255, _opacity * 255));
                    sb.End();      
                }                              
            }            
        }

        public SpriteBatch GetSpriteBatch()
        {
            SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            return spriteBatch;
        }

        public Rectangle GetBoundary()
        {
            return new Rectangle((int)_position.X, (int)_position.Y, (int)_size.X, (int)_size.Y);
        }
    }
}
